package me.lubinn.Incantatio;

import java.io.*;
import java.util.*;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityListener;

// Referenced classes of package me.orion.Incantatio:
//            Incantatio

public class IncantatioEntityListener extends EntityListener
{

    public static Incantatio plugin;

    public IncantatioEntityListener(Incantatio instance)
    {
        plugin = instance;
    }

    public void onEntityDamage(EntityDamageEvent event)
    {
        if(event.getEntity() instanceof Player)
        {
            String playername = "none";
            List<Player> players = event.getEntity().getWorld().getPlayers();
            for(Iterator<Player> iterator = players.iterator(); iterator.hasNext();)
            {
                Player player = iterator.next();
                if(player.getEntityId() == event.getEntity().getEntityId())
                {
                    playername = player.getName();
                }
            }

            try
            {
                FileInputStream in = new FileInputStream(Incantatio.IncantatioLog);
                Incantatio.prop.load(in);
                String LastSpell = Incantatio.prop.getProperty(playername);
                in.close();
                if(LastSpell.trim().equalsIgnoreCase("tueri"))
                {
                    event.setCancelled(true);
                    try
                    {
                        FileOutputStream out = new FileOutputStream(Incantatio.IncantatioLog);
                        Incantatio.prop.put(playername, "damage");
                        out.flush();
                        out.close();
                    }
                    catch(IOException ex)
                    {
                        ex.printStackTrace();
                    }
                } else
                if(LastSpell.trim().split(" ")[0].equalsIgnoreCase("celeritas") && event.getCause() == org.bukkit.event.entity.EntityDamageEvent.DamageCause.FALL)
                {
                    event.setCancelled(true);
                }
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
